﻿// <copyright file="ScriptArrayCollection.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-10-07</date>
// <summary>Script Array</summary>

namespace opensocial
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Browser;

    /// <summary>
    /// Script Array
    /// </summary>
    public class ScriptArrayCollection : IEnumerable<ScriptObject>
    {
        /// <summary>
        /// Initializes a new instance of the ScriptArrayCollection class.
        /// </summary>
        /// <param name="scriptObject">the script object</param>
        internal ScriptArrayCollection(ScriptObject scriptObject)
        {
            ScriptObject = scriptObject;
        }

        /// <summary>
        /// Gets or sets the script object
        /// </summary>
        public ScriptObject ScriptObject { get; set; }

        /// <summary>
        /// Gets the length of the script array
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "length", Justification = "Mirroring OpenSocial API.")]
        public int length
        {
            get
            {
                double length = (double) ScriptObject.GetProperty("length");

                return Convert.ToInt32(length);
            }
        }

        /// <summary>
        /// get at a specific index in the array
        /// </summary>
        /// <param name="index">the 0-based index</param>
        /// <returns>a script object</returns>
        public ScriptObject this[int index]
        {
            get
            {
                return this.ScriptObject.GetProperty(index) as ScriptObject;
            }
        }

        #region IEnumerable<ScriptObject> Members

        /// <summary>
        /// Gets the enumerator
        /// </summary>
        /// <returns>a new script array enumerator</returns>
        public IEnumerator<ScriptObject> GetEnumerator()
        {
            return new ScriptArrayEnumerator(this);
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Get the enumerator
        /// </summary>
        /// <returns>a new script array enumerator</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new ScriptArrayEnumerator(this);
        }

        #endregion
    }
}
